<template>
  <view class="form-overlay">
    <view class="form-container">
      <view class="form-header">
        <text>修改信息</text>
      </view>
      <view class="form-field">
        <text class="form-label">协议：</text>
        <picker mode="selector" :range="protocols" :value="protocolIndex" @change="onProtocolChange" :key="pickerKey" class="form-input">
          {{ editItem.protocol || '请选择协议' }}
        </picker>
      </view>
      <view class="form-field">
        <text class="form-label">入口域名：</text>
        <input v-model="editItem.domain" class="form-input" />
      </view>
      <view class="form-field">
        <text class="form-label">二级域名：</text>
        <input v-model="editItem.sld" class="form-input" />
      </view>
      <view class="form-field">
        <text class="form-label">跳转地址：</text>
        <input v-model="editItem.target" class="form-input" />
      </view>
      <view class="form-field">
        <text class="form-label">平台：</text>
        <input v-model="editItem.platform" class="form-input" />
      </view>
      <view class="form-buttons">
        <button @click="saveEdit" class="form-button save-button">保存</button>
        <button @click="cancelEdit" class="form-button cancel-button">取消</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    protocols: {
      type: Array,
      required: true
    },
    editItem: {
      type: Object,
      required: true,
	  protocol:""
    }
  },
  data() {
    return {
      pickerKey: 0 // 用于强制重新渲染
    };
  },
  computed: {
    protocolIndex() {
      return this.protocols.indexOf(this.editItem.protocol);
    }
  },
  methods: {
    onProtocolChange(e) {
      console.log('Selected protocol index:', e.detail.value);
      const selectedProtocol = this.protocols[e.detail.value];
      console.log('Selected protocol:', selectedProtocol);
      this.$emit('update:editItem', { ...this.editItem, protocol: selectedProtocol });
      this.editItem.protocol = selectedProtocol; // 更新 editItem.protocol
      this.pickerKey++; // 更新 key 值，强制重新渲染
    },
    saveEdit() {
      this.$emit('saveEdit');
    },
    cancelEdit() {
      this.$emit('cancelEdit');
    }
  }
};
</script>

<style scoped>
.form-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.form-container {
  background-color: white;
  padding: 20rpx;
  border-radius: 10rpx;
  width: 80%;
  box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.1);
}

.form-header {
  font-size: 30rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
  text-align: center;
}

.form-field {
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
}

.form-label {
  font-size: 24rpx;
  color: #666;
  margin-right: 10rpx;
  width: 100rpx;
  text-align: left;
}

.form-input {
  flex: 1;
  border: 1rpx solid #ccc;
  border-radius: 5rpx;
  padding: 10rpx;
  font-size: 24rpx;
}

.form-buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 20rpx;
}

.form-button {
  background-color: #007aff;
  color: white;
  border: none;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
  font-size: 24rpx;
  width: 48%;
  text-align: center;
}

.save-button {
  background-color: #007aff;
}

.cancel-button {
  background-color: #ff5555;
}
</style>